package stock.testSB;

import java.io.File;
import java.text.SimpleDateFormat;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

@Component
public class SBService implements SBServiceInterfalce {

	public static boolean flagb = false;

	@Override
	@Scheduled(cron = "0 0/1 * * * ?")
	public void getContent() {

		if (flagb == true) {
			System.out.println("上次没有计算完成，不参与计算");
			return;
		}
		flagb = true;
		try {
			System.out.println("计算股票是否有相关主要概念");
			LocalTime now1 = LocalTime.now();
			DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HHmmss");
			String formattedTime = now1.format(formatter);

			Calendar now = Calendar.getInstance();
			int hour = now.get(Calendar.HOUR_OF_DAY);

			if (hour >= 9 && hour <= 15) {
				// 这里放置在早上9点到下午3点（15点）之间需要执行的代码

			} else {
				return;
			}
			System.out.println("执行时进拿数据");
			for (int i = 0; i < SB1.listall.size(); i++) {
				int temp = SB1.listall.get(i).getZdfList().size();
				String code = SB1.listall.get(i).getName();
				if (SB1.listall.get(i).getZdfList().size() <= 0) {
					continue;
				}
				TimeZDF tz = SB1.listall.get(i).getZdfList().get(temp - 1);
				if (SB1.listall.get(i).getGn() != null) {
					boolean b = getTimeExc(SB1.listall.get(i).getTime());
					if (b == false) {
						String gn = getZTnote(code);
						SB1.listall.get(i).setTime(formattedTime);
						SB1.listall.get(i).setGn(gn);
						continue;
					}
					continue;
				}
				if (Float.parseFloat(tz.getZhangdiefu()) >= 3) {
					String gn = getZTnote(code);
					SB1.listall.get(i).setTime(formattedTime);
					SB1.listall.get(i).setGn(gn);
				}
			}
			System.out.println("计算SB当前概念结束");
		} catch (Exception e) {
			flagb = false;
			e.printStackTrace();
		}

		flagb = false;
	}

	public void setlistGn(String gn, String code) {

		for (int i = 0; i < SB1.listall.size(); i++) {
			if (code.equals(SB1.listall.get(i).getName())) {
				SB1.listall.get(i).setGn(gn);
				break;
			}
		}
	}

	public boolean getlistGn(String code) {

		for (int i = 0; i < SB1.listall.size(); i++) {
			if (code.equals(SB1.listall.get(i).getName())) {
				if (SB1.listall.get(i).getTime() != null) {
					return false;
				} else {
					return true;
				}
			}
		}
		return false;
	}

	public static boolean getTimeExc(String anotherTime) {

		if (anotherTime == null) {
			return false;
		}

		SimpleDateFormat sdf = new SimpleDateFormat("HHmmss");
		Date now = new Date();
		String currentTime = sdf.format(now);
		try {
			// 将当前时间字符串转换为分钟数
			int currentMinutes = Integer.parseInt(currentTime.substring(0, 2)) * 60 + Integer.parseInt(currentTime.substring(2, 4));
			int anotherMinutes = Integer.parseInt(anotherTime.substring(0, 2)) * 60 + Integer.parseInt(anotherTime.substring(2, 4));

			int difference = Math.abs(currentMinutes - anotherMinutes);
			if (difference > 10) {
				System.out.println("时间差大于10分钟");
				return false;
			} else {
				System.out.println("时间差小于等于10分钟");
			}
		} catch (NumberFormatException e) {
			e.printStackTrace();
		}
		return true;
	}

	public static String getZTnote(String code) {
		// 获取标签内容
		String all = "";
		try {

			// 创建WebClient实例，指定信任所有SSL/TLS证书的工厂
			String url = "https://www.cls.cn/stock?code=" + code;

			if (driver == null) {
				driver = getdriver();
			}
			driver.get(url);
			try {
				Thread.sleep(2000);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

			List<WebElement> webe = null;
			try {
				webe = driver.findElements(By.xpath("//div[@class='f-s-14 list-more-button more-button']"));

				webe.get(0).click();
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

			List<WebElement> elements = driver.findElements(By.xpath("//div[@class='b-c-e6e7ea telegraph-list']//a"));

			String str = "融资买入";
			int temp = 0;
			for (WebElement element : elements) {
				String content = element.getText();
				for (int i = 0; i < SB1.gn.size(); i++) {
					if (all.indexOf(SB1.gn.get(i)) > -1) {
						continue;
					}
					if (content.indexOf(str) > -1) {
						temp++;
					}
					if (content.indexOf(SB1.gn.get(i)) > -1) {
						all += SB1.gn.get(i) + " ";
					}
				}
			}
			all += " " + str + temp;

			System.out.println(code + all);
		} catch (Exception e) {
			System.out.println("-拿GN 网页发生错误--");
		}
		// driver.quit();
		return all;
	}

	public static WebDriver driver = null;

	public static WebDriver getdriver() {

		if (driver == null) {
			File f = new File("D:\\\\chromedriver\\\\chromedriver-win64133\\\\chromedriver.exe");
			if (f.exists()) {
				System.setProperty("webdriver.chrome.driver", "D:\\\\chromedriver\\\\chromedriver-win64133\\\\chromedriver.exe");
			} else {
				System.setProperty("webdriver.chrome.driver", "/opt/chromedriver-linux64/chromedriver");
			}
			// 指定Chrome浏览器核心

			// 创建Chrome浏览器驱动
			// 配置Chrome选项
			ChromeOptions options = new ChromeOptions();
			options.addArguments("--headless"); // 无界面运行
			options.addArguments("--no-sandbox");
			options.addArguments("--disable-gpu");
			options.addArguments("--disable-dev-shm-usage");
			// options.addArguments("--whitelisted-ips");
			// options.addArguments("--verbose");
			// options.addArguments("--allowed-ips");

			// 创建Chrome浏览器驱动
			driver = new ChromeDriver(options);
		}
		return driver;
	}
}
